using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestPlatform.Common;
using Newtonsoft.Json;
using RDJTProtocolServer;

namespace RawdataRDJTP
{
    public class CategoriesController
    {
        List<Category> Categories = new List<Category>();


        CategoriesController()
        {
            Categories.Add(new Category(1, "Beverages"));
            Categories.Add(new Category(2, "Condiments"));
            Categories.Add(new Category(3, "Confections"));
        }

        public void HandleRequest(RdjtpRequest request, RdjtpResponse response)
        {
            if (request.Method.Equals("read")) Read(request, response);
            else if (request.Method.Equals("create")) Create(request, response);
            else if (request.Method.Equals("update")) Create(request, response);
            else if (request.Method.Equals("delete")) Create(request, response);
            else response.Status = RdjtpStatus.GetText(RdjtpStatus.Error);
        }

        void Read(RdjtpRequest request, RdjtpResponse response)
        {
            if (request.Path.Equals("/categories"))
            {
                response.Body = JsonConvert.SerializeObject(Categories);
                response.Status = RdjtpStatus.GetText(RdjtpStatus.Ok);
            }
            else if (request.Path.Contains("/categories/"))
            {
                //ToDo - ErrorHandling - Ask if user forgot an ID?

                int cid = Int32.Parse(request.Path.Replace("/categories/", String.Empty));
                response.Body = JsonConvert.SerializeObject(Categories[cid]);
                response.Status = RdjtpStatus.GetText(RdjtpStatus.Ok);
            }
            else
            {
                response.Status = RdjtpStatus.GetText(RdjtpStatus.NotFound);
            }
        }

        void Create(RdjtpRequest request, RdjtpResponse response)
        {
            if (request.Path.Equals("/categories") && request.Body != null && !request.Body.Equals(""))
            {
                // ToDo - Check if valid JSON (ErrorHandling)

                // Test if count + 1 is the right way...
                Categories.Add(new Category(Categories.Count + 1, request.Body));
                
                response.Body = JsonConvert.SerializeObject(Categories[Categories.Count()]);
                response.Status = RdjtpStatus.GetText(RdjtpStatus.Created);
            }
            else
            {
                response.Status = RdjtpStatus.GetText(RdjtpStatus.BadRequest);
            }
        }

        void Update(RdjtpRequest request, RdjtpResponse response)
        {
            if (request.Path.Contains("/categories/"))
            {
                int cid = Int32.Parse(request.Path.Replace("/categories/", String.Empty));
                Categories[cid].Name = request.Body;
                response.Body = JsonConvert.SerializeObject(Categories[cid]);
                
                response.Status = RdjtpStatus.GetText(RdjtpStatus.Updated);
            }
            else
            {
                response.Status = RdjtpStatus.GetText(RdjtpStatus.BadRequest);
            }
        }

        void Delete(RdjtpRequest request, RdjtpResponse response)
        {
            if (request.Path.Contains("/categories/"))
            {
                int cid = Int32.Parse(request.Path.Replace("/categories/", String.Empty));
                if (cid < Categories.Count)
                {
                    Categories.RemoveAt(cid);
                    response.Status = RdjtpStatus.GetText(RdjtpStatus.Ok);
                }
                else
                {
                    response.Status = RdjtpStatus.GetText(RdjtpStatus.NotFound);
                }
            }
            else
            {
                response.Status = RdjtpStatus.GetText(RdjtpStatus.NotFound);
            }
        }
    }
}